//
//  WebViewController.m
//  LazyCat
//
//  Created by jike on 15/12/28.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "WebViewController.h"
#import "classifyViewController.h"
#import "AFNetworkHandler.h"

@interface WebViewController ()<UIWebViewDelegate,UIScrollViewDelegate,UIAlertViewDelegate,backNameAndIdDelegate,UITextFieldDelegate>
{
    NSString *_requestUUrl;
    UILabel *_tuiJianLabel;//推荐label，
    UILabel *topLable;
    UIImageView *twoImg; // 推荐图标
    UIView *_backView;
}
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UIButton *shareBtn;
@property (nonatomic, retain) UIView *navView;
@property (nonatomic, retain) UIImageView *oneImg; // 上架下架图片
@property (nonatomic) BOOL upDown; // 上架下架状态
@property (nonatomic, retain) UILabel *oneLable; // 上架lable

@end

@implementation WebViewController

-(void)backPassName:(NSString *)name nameId:(NSString *)nameID {//用于显示传递过来的数据
    self.className = name;
    self.classID = nameID;
    if (self.className.length < 1) {
        self.className = @"";
    }
    topLable.text = [NSString stringWithFormat:@"   所属分类  %@",self.className];
}
-(void)viewWillAppear:(BOOL)animated {
    NSString *url = [NSString stringWithFormat:@"%@/LCat/Product-%@?appname=lanmao&plat=ios&username=%@&iszhanggui=1&sign2=%@",BaseUrl,self.requestUrl,[AppData share].username,[Utils md5SignStr]];
    _requestUUrl = url;
    
    //[self createWebView];
}

#pragma mark -- 键盘高度
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
    
    CGRect inputFieldRect = _backView.frame;
    
    //    if (inputFieldRect.origin.y == ScreenHeight - 150) {
    //        if (_textField1.editing) {
    //            inputFieldRect.origin.y += yOffset;
    //        }
    //    }else {
    inputFieldRect.origin.y += yOffset;
    //    }
    [UIView animateWithDuration:duration animations:^{
        _backView.frame = inputFieldRect;
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];//键盘高度变化通知
    [self createWebView];
    [self createBottomView];
}
#pragma mark - webView
-(void)createWebView {
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 44)];
    self.webView.delegate = self;
    self.webView.backgroundColor = BackColor;
    self.webView.scrollView.delegate = self;
    [self.webView.scrollView setBounces:YES];
    [self.webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    NSString *url = [NSString stringWithFormat:@"%@/LCat/Product-%@?appname=lanmao&plat=ios&username=%@&iszhanggui=1&sign2=%@",BaseUrl,self.requestUrl,[AppData share].username,[Utils md5SignStr]];
    _requestUUrl = url;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_requestUUrl]]];// 加载请求
    [self.view addSubview:self.webView];
    self.navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];// 导航View
    self.navView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelBtn.frame = CGRectMake(5, 20, 40, 40);// 返回button
    self.cancelBtn.backgroundColor = [UIColor darkGrayColor];
    self.cancelBtn.layer.cornerRadius = 20;
    self.cancelBtn.layer.masksToBounds = YES;
    self.cancelBtn.alpha = 0.6;
    [self.cancelBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.cancelBtn];
    UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(18, 30, 12, 21)];
    imv.image = [UIImage imageNamed:@"lALOAy2ooyYW_22_38"];
    [self.navView addSubview:imv];
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];// 分享butto
    self.shareBtn.frame = CGRectMake(ScreenWidth-45, 20, 40, 40);
    self.shareBtn.backgroundColor = [UIColor darkGrayColor];
    self.shareBtn.layer.cornerRadius = 20;
    self.shareBtn.layer.masksToBounds = YES;
    self.shareBtn.alpha = 0.6;
    [self.shareBtn addTarget:self action:@selector(fenXiangAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.shareBtn];
    UIImageView *sharimg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 35, 30, 20, 20)];
    sharimg.image = [UIImage imageNamed:@"lALOAy2opCYm_38_38"];
    [self.navView addSubview:sharimg];
    
}
-(void)createBottomView{// 底部view
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, ScreenHeight - 44, ScreenWidth, 44);
    [btn setTitle:@"编辑" forState:UIControlStateNormal];
    btn.backgroundColor = MainColorT;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 215)];
    _backView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_backView];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 35, ScreenWidth, 180)];
    bottomView.backgroundColor = BackColor;
    [_backView addSubview:bottomView];
    UIView *bottomView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    bottomView2.backgroundColor = [UIColor whiteColor];
    [_backView addSubview:bottomView2];
    //
    NSArray *arr = @[@"售价",@"成本",@"库存"];
    for (int i = 0;i < 3; i++ ) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10 + ScreenWidth/3 * i, 3, 30, 30)];
//        label.backgroundColor = [UIColor blueColor];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = SetColor(80, 80, 80, 1);
        label.text = arr[i];
        [bottomView2 addSubview:label];
        
        UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(40 + ScreenWidth/3 * i, 3, 60, 30)];
//        textF.backgroundColor = MainColorT;
        textF.tag = 300 + i;
        textF.delegate = self;
        textF.font = [UIFont systemFontOfSize:13];
        textF.textColor = SetColor(80, 80, 80, 1);
        textF.borderStyle = UITextBorderStyleRoundedRect;
        textF.keyboardType = UIKeyboardTypeDecimalPad;
        [bottomView2 addSubview:textF];
        if (i == 0) {//售价
            textF.text = _shoujiaStr;
        }else if (i == 1){//成本
            textF.text = _chengbenStr;
        }else if (i == 2){//库存
            textF.text = _kucunStr;
        }
    }
    
    topLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, ScreenWidth, 44)];
    topLable.backgroundColor = [UIColor whiteColor];
    topLable.font = [UIFont fontWithName:nil size:15];
    topLable.text = [NSString stringWithFormat:@"   所属分类  %@",self.className];
    [bottomView addSubview:topLable];
    topLable.userInteractionEnabled = YES;
    UITapGestureRecognizer *toplableTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topLableTapAction)];
    [topLable addGestureRecognizer:toplableTap];
    UIImageView *jtImg = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 25, 15, 8, 15)];
    jtImg.image = [UIImage imageNamed:@"go_log.png"];
    [topLable addSubview:jtImg];
    UIView *minView = [[UIView alloc] initWithFrame:CGRectMake(0, 46, ScreenWidth, 90)];// 45 x 45
    minView.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:minView];
    float whith = ScreenWidth/3;
    // 上下架
    self.oneImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith/2-23, 10, 45, 45)];// 下架
    self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
    [minView addSubview:self.oneImg];
    self.oneLable = [[UILabel alloc]initWithFrame:CGRectMake(whith/2-23, 60, 45, 20)];
    self.oneLable.textAlignment = NSTextAlignmentCenter;
    self.oneLable.font = [UIFont fontWithName:nil size:14];
    if ([_isShangJia isEqualToString:@"1"]) {
        self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
        self.oneLable.text = @"上架";
    }else {
        self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
        self.oneLable.text = @"下架";
    }
    [minView addSubview:self.oneLable];
    self.oneImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *downjia = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(downAction)];
    [self.oneImg addGestureRecognizer:downjia];
    // 推荐
    twoImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith+whith/2-23, 10, 45, 45)];
    
    [minView addSubview:twoImg];
    _tuiJianLabel = [[UILabel alloc]initWithFrame:CGRectMake(whith+whith/2-23, 60, 45, 20)];
    _tuiJianLabel.textAlignment = NSTextAlignmentCenter;
    _tuiJianLabel.font = [UIFont fontWithName:nil size:14];
    if ([_isHot isEqualToString:@"1"]) {
        _tuiJianLabel.text = @"已推荐";
        twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
    }else {
        _tuiJianLabel.text = @"推荐";
        twoImg.image = [UIImage imageNamed:@"xqzan.png"];
    }
    [minView addSubview:_tuiJianLabel];
    twoImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tuijian = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuijianAction)];
    [twoImg addGestureRecognizer:tuijian];
    UIImageView *threeImg = [[UIImageView alloc] initWithFrame:CGRectMake(whith+whith+whith/2-23, 10, 45, 45)];
    threeImg.image = [UIImage imageNamed:@"xqfenxiang.png"];// 分享
    [minView addSubview:threeImg];
    UILabel *threeLable = [[UILabel alloc]initWithFrame:CGRectMake(whith+whith+whith/2-23, 60, 45, 20)];
    threeLable.textAlignment = NSTextAlignmentCenter;
    threeLable.font = [UIFont fontWithName:nil size:14];
    threeLable.text = @"分享";
    [minView addSubview:threeLable];
    threeImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *fenxiang = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fenXiangAction)];
    [threeImg addGestureRecognizer:fenxiang];
//    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    saveBtn.frame = CGRectMake(ScreenWidth/2 - 60, 143, 120, 30);// 保存按钮
//    saveBtn.backgroundColor = MainColor;
//    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
//    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];//保存事件
//    saveBtn.layer.cornerRadius = 3;
//    saveBtn.clipsToBounds = YES;
//    [bottomView addSubview:saveBtn];
    NSArray *arrBtn = @[@"保存",@"取消"];
    for (int i = 0; i < 2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(ScreenWidth/2 * i, 136, ScreenWidth/2, 44);
        if (i == 0) {
            btn.backgroundColor = MainColorT;
        }else {
            btn.backgroundColor = MainColorOld;
        }
        btn.tag = 320 + i;
        [btn addTarget:self action:@selector(saveOrCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arrBtn[i] forState:UIControlStateNormal];
        [bottomView addSubview:btn];
    }
}
#pragma mark -- 收键盘...
- (void)hideTheKeyBoard {
    [_backView endEditing:YES];
    [_backView resignFirstResponder];
}
#pragma mark -- 编辑按钮点击事件
- (void)editBtnClick:(UIButton *)sender {
    [UIView animateWithDuration:0.2 animations:^{
        _backView.frame = CGRectMake(0, ScreenHeight - 215, ScreenWidth, 215);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.webView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - 215);
        }];
    }];
}
#pragma mark --HideTheView 
- (void)hideTheEditView {
    [self.view endEditing:YES];
    [_backView endEditing:YES];
    [UIView animateWithDuration:0.2 animations:^{
        _backView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 215);
        self.webView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - 44);
    }];
}
#pragma mark -- 保存or取消...
- (void)saveOrCancelBtnClick:(UIButton *)sender {
    if (sender.tag == 320) {//保存
        #define saveProductUrl BaseUrl @"/WPT-OpenAPI?action=UpdateProductCategory&control=LCatBusiness&Categoryid=%@&Id=%@&Username=%@&stock=%@&sale=%@&cost=%@"
        UITextField *shoujiaT = (UITextField *)[_backView viewWithTag:300];
        UITextField *chengbenT = (UITextField *)[_backView viewWithTag:301];
        UITextField *kuncunT = (UITextField *)[_backView viewWithTag:302];
        _shoujiaStr = shoujiaT.text;
        _chengbenStr = chengbenT.text;
        _kucunStr = kuncunT.text;
        if ([_shoujiaStr isEqualToString:@""] || [_chengbenStr isEqualToString:@""] || [_kucunStr isEqualToString:@""]) {
            [[MessShowView share]showWithMess:@"修改的信息不能为空"];
            return;
        }
        NSString *url = [NSString stringWithFormat:saveProductUrl,self.classID,self.requestUrl,[AppData share].username,_kucunStr,_shoujiaStr,_chengbenStr];
        [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
            NSDictionary *dic = result;
            NSString *r = dic[@"r"];
            if ([r isEqualToString:@"T"]) {
                [[MessShowView share] showWithMess:@"保存成功" login:YES];
                [AppData share].isLoadd = YES;//返回刷新
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [[MessShowView share] showWithMess:@"保存失败" login:NO];
            }
        }];
    }else if (sender.tag == 321){//取消
        [self hideTheEditView];
    }
}
//-(void)saveAction {//保存事件
//    //#define saveProductUrl BaseUrl @"/WPT-OpenAPI?categoryid=%@&id=%@&Control=LCatBusiness&Action=UpdateProductCategory&sale=%@&cost=%@&stock=%@"
//    }
-(void)topLableTapAction {// 跳转所有分类页面
    classifyViewController *classVC = [[classifyViewController alloc]init];
    classVC.name = self.className;
    classVC.nameId = self.classID;
    classVC.delegate = self;
    [self.view endEditing:YES];
    [_backView endEditing:YES];
    [self.navigationController pushViewController:classVC animated:YES];
}
-(void)downAction {// 上、下架
    if ([_isShangJia isEqualToString:@"0"]) { // 上架
//        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:nil message:@"确定下架" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertV.tag = 111;
//        [alertV show];
        NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"0"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
                self.oneLable.text = @"上架";
                self.upDown = YES;
                _isShangJia = @"1";
                [AppData share].isLoadd = YES;//返回刷新
                [[MessShowView share]showWithMess:@"商品下架成功" login:YES];
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }else{ // 下架
//        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:nil message:@"确定上架" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertV.tag = 112;
//        [alertV show];
        NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"1"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                self.upDown = NO;
                self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
                self.oneLable.text = @"下架";
                _isShangJia = @"0";
                [AppData share].isLoadd = YES;//返回刷新
                [[MessShowView share]showWithMess:@"商品上架成功" login:YES];
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }
}
#pragma mark -- uialertView代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 111) {//下架
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"0"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"商品下架成功" login:YES];
//                    self.oneImg.image = [UIImage imageNamed:@"xqshangjia.png"];
//                    self.oneLable.text = @"上架";
//                    self.upDown = YES;
//                    _isShangJia = @"1";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 112){
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:shangXiaJiaProduct,_requestUrl,@"1"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"商品上架成功" login:YES];
//                    self.upDown = NO;
//                    self.oneImg.image = [UIImage imageNamed:@"xqxiajia.png"];
//                    self.oneLable.text = @"下架";
//                    _isShangJia = @"0";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 113){//取消推荐
        if (buttonIndex == 1) {
//            NSString *url = [NSString stringWithFormat:isRecommend,_requestUrl,@""];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
//                    _tuiJianLabel.text = @"推荐";
//                    twoImg.image = [UIImage imageNamed:@"xqzan.png"];
//                    _isHot = @"0";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }else if (alertView.tag == 114){//推荐
        if (buttonIndex == 1) {
//            NSString *url= [NSString stringWithFormat:isRecommend,_requestUrl,@"1"];
//            [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
//                NSDictionary *dic = networkRequest.responseData;
//                NSString *result = [dic objectForKey:@"m"];
//                if ([result isEqualToString:@"success"]) {
//                    [[MessShowView share]showWithMess:@"操作成功" login:YES];
//                    _tuiJianLabel.text = @"已推荐";
//                    twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
//                    _isHot = @"1";
//                }else  {
//                    [[MessShowView share]showWithMess:result login:NO];
//                }
//            }];
        }
    }
}
#pragma mark - 推荐
-(void)tuijianAction {
    NSString *message;
    if ([_isHot isEqualToString:@"1"]) {
        message = @"取消推荐";
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertView.tag = 113;
//        [alertView show];
        NSString *url = [NSString stringWithFormat:isRecommend,_requestUrl,@""];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                [[MessShowView share]showWithMess:@"操作成功" login:YES];
                _tuiJianLabel.text = @"推荐";
                twoImg.image = [UIImage imageNamed:@"xqzan.png"];
                _isHot = @"0";
                [AppData share].isLoadd = YES;//返回刷新
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }else {
//        message = @"确认推荐";
//        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
//        alertView.tag = 114;
//        [alertView show];
        NSString *url= [NSString stringWithFormat:isRecommend,_requestUrl,@"1"];
        [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
            NSDictionary *dic = networkRequest.responseData;
            NSString *result = [dic objectForKey:@"m"];
            if ([result isEqualToString:@"success"]) {
                [[MessShowView share]showWithMess:@"操作成功" login:YES];
                _tuiJianLabel.text = @"已推荐";
                twoImg.image = [UIImage imageNamed:@"tuijian1.png"];
                _isHot = @"1";
                [AppData share].isLoadd = YES;//返回刷新
            }else  {
                [[MessShowView share]showWithMess:result login:NO];
            }
        }];
    }
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 分享
-(void)fenXiangAction {
    
    NSString *url = [NSString stringWithFormat:@"%@&sharedUser=%@",_requestUUrl,[AppData share].username];
    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",self.PName,url] withImgUrl:self.PImg url:url];
//    NSString *string = self.requestUrl;
//    NSArray *array = [string componentsSeparatedByString:@"?"];
//    NSString *ur = array[0];
//    NSString *url = [NSString stringWithFormat:@"%@?sharedUser=%@",ur,[AppData share].username];
//    [[ShareView share] showMess:[NSString stringWithFormat:@"%@%@",@"名字",url] withImgUrl:@"图片url" url:url];
}
#pragma mark - 返回顶部
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point = scrollView.contentOffset;
    if (point.y > 100) { // 搜索view背景颜色
        self.navView.backgroundColor = [UIColor colorWithRed:255/255.0 green:198/255.0 blue:0/255.0 alpha:1];
        self.navView.alpha = point.y / 150;
        self.cancelBtn.backgroundColor = [UIColor clearColor];
        self.shareBtn.backgroundColor = [UIColor clearColor];
    }else{
        self.navView.backgroundColor = [UIColor clearColor];
        self.cancelBtn.backgroundColor = [UIColor darkGrayColor];
        self.cancelBtn.alpha = 0.6;
        self.shareBtn.backgroundColor = [UIColor darkGrayColor];
        self.shareBtn.alpha = 0.6;
    }
}
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [AppData share].isLoad = YES;
//}
#pragma mark -- 词频所有词变更判断
- (void)textfieldChange:(UITextField *)textField {
    NSLog(@"1234567890-");
    if (textField.tag == 72) {
        if ([self validateNumber:textField.text]) {
            _shoujiaStr = textField.text;
        }else {
            textField.text = _shoujiaStr;
        }
    }else if (textField.tag == 73){
        if ([self validateNumber:textField.text]) {
            _chengbenStr = textField.text;
        }else {
            textField.text = _chengbenStr;
        }
    }else if (textField.tag == 74){
        if ([self validateNumber:textField.text]) {
            _kucunStr = textField.text;
        }else {
            textField.text = _kucunStr;
        }
    }
}
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //if (textField == _textField1) {
    NSScanner      *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *numbers;
    NSRange         pointRange = [textField.text rangeOfString:@"."];
    
    if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) )
    {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }
    else
    {
        //        if (textField == _textField1) {
        //            numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        //        }else {
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
        //        }
    }
    
    if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] )
    {
        return NO;
    }
    
    short remain = 2; //默认保留2位小数
    
    NSString *tempStr = [textField.text stringByAppendingString:string];
    NSUInteger strlen = [tempStr length];
    if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
        if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时，如果再输入“.”则被视为无效。
            return NO;
        }
        if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”，当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数，则视当次输入无效。
            return NO;
        }
    }
    NSRange zeroRange = [textField.text rangeOfString:@"0"];
    if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
        if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时，再输入不为“0”或者“.”的字符时，则将此输入替换输入框的这唯一字符。
            textField.text = string;
            return NO;
        }else{
            if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时，并且没有“.”字符时，如果当此输入的字符为“0”，则视当此输入无效。
                if([string isEqualToString:@"0"]){
                    return NO;
                }
            }
        }
    }
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) )
    {
        return NO;
    }
    //}
    return YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
